﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// Java 列表显示类
    /// </summary>
    public class JavaResourcesMapperHelper
    {

        /// <summary>
        /// 根据表名创建列表显示类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateViewMapperClass(string strFilePath,string strProjectName, string strTableName,string strTableComment)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id
            string strJavaPrimaryKey = CommonHelper.GetTableNameUpper(strPrimaryKey);
            strJavaPrimaryKey = CommonHelper.GetTableNameUpper(strJavaPrimaryKey);//News
            strJavaPrimaryKey = CommonHelper.GetTableNameFirstLowerSecondUpper(strJavaPrimaryKey);

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);//News
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            string strTableComment2 = CommonHelper.GetColumnKeyComment(strTableComment);

            string strNewPath = strFilePath + "\\resources\\mapper";
            if (!Directory.Exists(strNewPath))
            {
                Directory.CreateDirectory(strNewPath);
            }

            var utf8WithoutBom = new System.Text.UTF8Encoding(false);
            StreamWriter sw = new StreamWriter(strNewPath + "\\" + strClassName + "Mapper.xml", false, utf8WithoutBom);

            //*****************************************************************
            //注意生成文件时必须严格遵守XML规则，隔空或换行都会导致数据读取失败
            //*****************************************************************
            sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sw.WriteLine("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\">");
            sw.WriteLine("<mapper namespace=\"com.toonan." + strTableNameLower + ".mapper." + strClassName + "Mapper\">");
            sw.WriteLine("");
            sw.WriteLine("    <!-- 通用查询映射结果 -->");
            sw.WriteLine("    <resultMap id=\"BaseResultMap\" type=\"com.toonan." + strTableNameLower + ".model." + strClassName + "\">");
            sw.WriteLine("        <id column=\""+strPrimaryKey+"\" property=\""+ strJavaPrimaryKey + "\" />");
            sw.WriteLine(CommonHelper.GetColumnListResultForJava(strTableName));
            sw.WriteLine("    </resultMap>");
            sw.WriteLine("    ");
            sw.WriteLine("    <!-- 通用查询结果列 -->");
            sw.WriteLine("    <sql id=\"Base_Column_List\">");
            sw.WriteLine("    "+ CommonHelper.GetColumnListForJava(strTableName));
            sw.WriteLine("    </sql>");
            sw.WriteLine("");
            sw.WriteLine("<!-- 根据Dto查询并返回数据持久化对象集合 -->");
            sw.WriteLine("	<select id=\"list\" parameterType=\"Map\" resultType=\"com.toonan." + strTableNameLower + ".model." + strClassName + "\">");
            sw.WriteLine("		SELECT");
            sw.WriteLine("			<include refid=\"Base_Column_List\" />	");
            sw.WriteLine("		FROM "+ strTableName);
            sw.WriteLine("		<where>");
            sw.WriteLine("		    <include refid=\"equal\" />");
            sw.WriteLine("		</where>");
            sw.WriteLine("		<if test=\"_order != null and _order != ''\">");
            sw.WriteLine("		     ORDER BY ${_order}");
            sw.WriteLine("		</if>			");
            sw.WriteLine("	</select>");
            sw.WriteLine("");
            sw.WriteLine("	<!-- 根据Dto查询并返回分页数据持久化对象集合 -->");
            sw.WriteLine("	<select id=\"listPage\" parameterType=\"Map\" resultType=\"com.toonan." + strTableNameLower + ".model." + strClassName + "\" useCache=\"false\">");
            sw.WriteLine("	    SELECT");
            sw.WriteLine("			<include refid=\"Base_Column_List\" />	");
            sw.WriteLine("		FROM "+ strTableName);
            sw.WriteLine("		<where>");
            sw.WriteLine("		    <include refid=\"equal\" />");
            sw.WriteLine("		</where>	");
            sw.WriteLine("		<if test=\"_order != null and _order != ''\">");
            sw.WriteLine("		     ORDER BY ${_order}");
            sw.WriteLine("		</if>		");
            sw.WriteLine("	</select>");
            sw.WriteLine("	");
            sw.WriteLine("	<!-- 根据Dto模糊查询并返回数据持久化对象集合(字符型字段模糊匹配，其余字段精确匹配) -->");
            sw.WriteLine("	<select id=\"like\" parameterType=\"Map\" resultType=\"com.toonan." + strTableNameLower + ".model." + strClassName + "\">");
            sw.WriteLine("		SELECT");
            sw.WriteLine("			<include refid=\"Base_Column_List\" />	");
            sw.WriteLine("		FROM "+ strTableName);
            sw.WriteLine("		<where>");
            sw.WriteLine("		    <include refid=\"like\" />");
            sw.WriteLine("		</where>");
            sw.WriteLine("		<if test=\"_order != null and _order != ''\">");
            sw.WriteLine("		     ORDER BY ${_order}");
            sw.WriteLine("		</if>			");
            sw.WriteLine("	</select>");
            sw.WriteLine("");
            sw.WriteLine("	<!-- 根据Dto模糊查询并返回分页数据持久化对象集合(字符型字段模糊匹配，其余字段精确匹配) -->");
            sw.WriteLine("	<select id=\"likePage\" parameterType=\"Map\" resultType=\"com.toonan." + strTableNameLower + ".model." + strClassName + "\" useCache=\"false\">");
            sw.WriteLine("	    SELECT");
            sw.WriteLine("			<include refid=\"Base_Column_List\" />	");
            sw.WriteLine("		FROM "+ strTableName);
            sw.WriteLine("		<where>");
            sw.WriteLine("		    <include refid=\"like\" />");
            sw.WriteLine("		</where>");
            sw.WriteLine("		<if test=\"_order != null and _order != ''\">");
            sw.WriteLine("		     ORDER BY ${_order}");
            sw.WriteLine("		</if>			");
            sw.WriteLine("	</select>");
            sw.WriteLine("	<!-- 根据数学表达式进行数学运算 -->");
            sw.WriteLine("	<select id=\"calc\" parameterType=\"Map\" resultType=\"String\">");
            sw.WriteLine("		SELECT");
            sw.WriteLine("			${_expr}");
            sw.WriteLine("		FROM "+ strTableName);
            sw.WriteLine("		<where>");
            sw.WriteLine("		    <include refid=\"equal\" />");
            sw.WriteLine("		</where>");
            sw.WriteLine("	</select>");
            sw.WriteLine("    <!-- Where精确匹配字段 -->");
            sw.WriteLine("    <sql id=\"equal\">");
            sw.WriteLine(CommonHelper.GetSearchEqualListForJava(strTableName));
            sw.WriteLine("    </sql>");
            sw.WriteLine("");
            sw.WriteLine("");
            sw.WriteLine("");
            sw.WriteLine("    <!-- Where模糊匹配字段 -->");
            sw.WriteLine("    <sql id=\"like\">");
            sw.WriteLine(CommonHelper.GetSearchLikeListForJava(strTableName));
            sw.WriteLine("    </sql>");
            sw.WriteLine("");
            sw.WriteLine("</mapper>");

            sw.Close();

        }



        /// <summary>
        /// 获取搜索字段列表
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetSearchItemList(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释


                    if (strColumnComment.Contains("{搜索}"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "                <div class=\"layui-inline\">\r\n";
                        strValue += "                    <label class=\"layui-form-label\">" + strColumnComment2 + "</label>\r\n";
                        strValue += "                    <div class=\"layui-input-inline\">\r\n";
                        strValue += "                        <input type=\"text\" id=\"" + strColumnName + "\" name=\"" + strColumnName + "\" placeholder=\"请输入" + strColumnComment2 + "\" autocomplete=\"off\" class=\"layui-input\">\r\n";
                        strValue += "                    </div>\r\n";
                        strValue += "                </div>\r\n";

                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取表格显示字段列表
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetTableColsList(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释

                    string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                    string strValue = "                {\r\n";
                    strValue += "                    field: \"" + strColumnName + "\",\r\n";
                    strValue += "                    title: \"" + strColumnComment2 + "\",\r\n";
                    if (strColumnName == "created_at" || strColumnKey == "PRI")
                    {
                        //主键和日期需要排序
                        strValue += "                    sort: !0,\r\n";
                    }
                    if (strColumnKey == "PRI")
                    {
                        //主键设置宽度
                        strValue += "                    width: 80,\r\n";
                    }
                    if (strColumnName == "created_at")
                    {
                        //日期设置宽度及格式
                        strValue += "                    width: 180,\r\n";
                        strValue += "                    templet: \"<div>{{layui.util.toDateString(d.created_at, 'yyyy-MM-dd HH:mm')}}</div>\",\r\n";
                    }
                    strValue += "                },\r\n";

                    strReturnValue += strValue;

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }


    }
}
